import axios from 'axios';
import {ElLoading} from 'element-plus'

axios.defaults.baseURL = 'http://' //测试
//post请求头
axios.defaults.headers.post["Content-Type"] =
    "application/x-www-form-urlencoded;charset=UTF-8";
//设置超时
axios.defaults.timeout = 10000;

let loading

//开启loading
const startLoading = () => {
    const option = {
        lock: true,
        text: "加载中....",
        background: 'rgba(0,0,0,0.7)'
    }
    loading = ElLoading.service(option);
}
//关闭loading
const endLoading = () => {
    loading.close();
}

//请求拦截
axios.interceptors.request.use(config => {
        startLoading()
        return config
    }, error => {
        return Promise.reject(error);
    }
)

//响应拦截
axios.interceptors.response.use(response => {
        endLoading()
        return response;
    }, error => {
        endLoading()
        return Promise.reject(error)
    }
)
export default axios;